package com.tjvib.base;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;

import com.tjvib.common.RetryCallback;
import com.tjvib.util.LogUtil;
import com.tjvib.view.dialog.HintDialog;


public abstract class BaseFragment extends Fragment {

    private View view;


    @Override
    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {

        view = inflater.inflate(getContentView(), container, false);

        //初始化控件
        initView();

        //设置数据
        initData();

        return view;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    public View getView() {
        if (view != null)
            return view;

        LogUtil.e("getView view == null");
        return null;
    }

    protected abstract int getContentView();

    protected abstract void initView();

    protected abstract void initData();


    public void showErrorDialog(String message) {
        getActivity().runOnUiThread(() -> {
            HintDialog hintDialog = new HintDialog(getContext());
            hintDialog.showDialog(getContext(), "错误", message,
                    view -> hintDialog.dismiss(),
                    view -> hintDialog.dismiss());
        });
    }

    public void showHintDialog(String message) {
        getActivity().runOnUiThread(() -> {
            HintDialog hintDialog = new HintDialog(getContext());
            hintDialog.showDialog(getContext(), "提示", message,
                    view -> hintDialog.dismiss(),
                    view -> hintDialog.dismiss());
        });
    }
}
